import useSelectStore from "@/store/modules/select";
import { listTagByPage } from "@/api/blog/tag.js";

const SelectTypeMap = [
  {
    name: "category",
    callback: listTagByPage,
    labelProp: "categoryName",
    valueProp: "categoryId",
  },
  {
    name: "tag",
    callback: listTagByPage,
    labelProp: "tagName",
    valueProp: "tagId",
  },
];

/**
 * 获取一些选项数据
 */
export function useSelect(...args) {
  const res = ref({});
  return (() => {
    args.forEach((selectType, index) => {
      //分类选项
      res.value[selectType] = [];
      const selects = useSelectStore().getSelect(selectType);
      if (selects) {
        res.value[selectType] = selects;
      } else {
        const findItem = SelectTypeMap.find((mapItem) => {
          return mapItem.name === selectType;
        });
        if (!findItem) {
          return null;
        }
        // findItem.callback().then((resp) => {
        //   res.value[selectType] = resp.data.map((p) => ({
        //     label: p[findItem.labelProp],
        //     value: p[findItem.valueProp],
        //   }));
        //   useSelectStore().setSelect(selectType, res.value[selectType]);
        // });
      }
    });
    return toRefs(res.value);
  })();
}
